package bancoperu.rest;


import java.util.Date;

import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import net.sf.json.JSONObject;
import bancoperu.dao.AccountDAO;
import bancoperu.dao.TransactionDAO;
import bancoperu.model.Account;
import bancoperu.model.Customer;
import bancoperu.model.Transaction;

@Path("/transaction")
public class TransactionRest {
	
	@POST
	@Path("/deposit")
	@Produces(MediaType.TEXT_PLAIN)
	public String savedeposit( @FormParam("AccountType") int AccountType,
								 @FormParam("Amount") Double Amount,
								 @FormParam("TypeTransaction") int TypeTransaction,
								 @FormParam("Customerid") String CustomerID) {		
		
		System.out.println("AccountType: " + AccountType);
		System.out.println(Amount);
		System.out.println("CustomerID: " + CustomerID);
		System.out.println(TypeTransaction);
		
		JSONObject objeto = new JSONObject();
		
		try {
			
			Customer voCus = new Customer();
			voCus.setCustomerId(CustomerID);
			
			AccountDAO aDao = new AccountDAO();
			String accountId = aDao.GetAccountID(CustomerID, AccountType);
			
			
			// ok			
//			System.out.println("accountId: "  + accountId);
			
			Account acc = new Account();
			acc.setAccountID(accountId);
			
									
			Transaction vo = new Transaction();
			vo.setAccount(acc);
			vo.setType(TypeTransaction);
			vo.setTransaction(new Date());
			vo.setAmount(Amount);
			vo.setCustomer(voCus);
			
			TransactionDAO tdao = new TransactionDAO();
			
			//System.out.println("Before insert");
			// insertar la transaccion
			tdao.insert(vo);
//			System.out.println("After insert");
			//acc.setAccountBalance(acc.getAccountBalance() + vo.getAmount());
			
			// actualizar la monto de la cuenta
			
//			acc.setAccountBalance(aDao.GetAccountBalance2(accountId, voCus.getCustomerId()));
			Account currentAccountObject = aDao.GetAccountBalance2(CustomerID, AccountType);
			double currentBalance = currentAccountObject.getAccountBalance();
			
			
			// UPDATE account SET AccountBalance=? WHERE CustomerID = ? and AccountType=?
			
			Account newAccount = new Account();
			newAccount.setAccountBalance(currentBalance + Amount);
			newAccount.setCustomer(voCus);
			newAccount.setAccountType(AccountType);
			
			
//			if(aDao.update(acc.getAccountBalance() + Amount, accountId, voCus.getCustomerId()))
//					System.out.println("UPDATED BALANCE!!!!!!!!!!!!!!!!!!");
//			System.out.println("Updated AccountDAO!!!!!!!");
			
			aDao.update(newAccount);
			
			objeto.put("status", "true");
//			System.out.println("PUT!!!!!!!!");
			
		
		} catch (Exception e) {			
			System.out.println(e.getMessage());			
			objeto.put("status", "false");
		}
		return objeto.toString();
	}
	
	@POST
	@Path("/withdraw")
	@Produces(MediaType.TEXT_PLAIN)
	public String savewithdraw( @FormParam("AccountType") int AccountType,
								 @FormParam("Amount") Double Amount,
								 @FormParam("TypeTransaction") int TypeTransaction,
								 @FormParam("Customerid") String CustomerID) {		
		
		System.out.println(AccountType);
		System.out.println(Amount);
		System.out.println(CustomerID);
		System.out.println(TypeTransaction);
		
		JSONObject objeto = new JSONObject();
		
		try {
			
			Customer voCus = new Customer();
			voCus.setCustomerId(CustomerID);
			
			AccountDAO aDao = new AccountDAO();
			String accountId = aDao.GetAccountID(CustomerID, AccountType);
			
			
			// ok			
//			System.out.println("accountId: "  + accountId);
			
			Account acc = new Account();
			acc.setAccountID(accountId);
			
									
			Transaction vo = new Transaction();
			vo.setAccount(acc);
			vo.setType(TypeTransaction);
			vo.setTransaction(new Date());
			vo.setAmount(Amount);
			vo.setCustomer(voCus);
			
			TransactionDAO tdao = new TransactionDAO();
			
			//System.out.println("Before insert");
			// insertar la transaccion
			tdao.insert(vo);
//			System.out.println("After insert");
			//acc.setAccountBalance(acc.getAccountBalance() + vo.getAmount());
			
			Account currentAccountObject = aDao.GetAccountBalance2(CustomerID, AccountType);
			double currentBalance = currentAccountObject.getAccountBalance();
			
			
			// UPDATE account SET AccountBalance=? WHERE CustomerID = ? and AccountType=?
			
			Account newAccount = new Account();
			newAccount.setAccountBalance(currentBalance - Amount);
			newAccount.setCustomer(voCus);
			newAccount.setAccountType(AccountType);
			
			
//			if(aDao.update(acc.getAccountBalance() + Amount, accountId, voCus.getCustomerId()))
//					System.out.println("UPDATED BALANCE!!!!!!!!!!!!!!!!!!");
//			System.out.println("Updated AccountDAO!!!!!!!");
			
			aDao.update(newAccount);
			
			objeto.put("status", "true");
//			System.out.println("PUT!!!!!!!!");
			
		
		} catch (Exception e) {			
			System.out.println(e.getMessage());			
			objeto.put("status", "false");
		}
		return objeto.toString();
	}
	
	
}
